import BasePageCtrl, { AnimType } from "./BasePageCtrl";
import UiManager from "./UiManage";
import n from "./AudioManager"
import l from "./PlayerDataSys"
import s from "./PeripheralSystemDataMgr"
import c from "./EventMgr"
import u from "./GameEventType"
import p from "./frameworkManager"
import d from "./AdManager"
import h from "./EngineUtil"
import f from "./BasePageCtrl"
import g from "./PageMgr"
import y, { TravelEvent } from "./PeripheralSystemMgr"
import m from "./UiManage"
import _ from "./TravelEventRewardPage"

const { ccclass, property } = cc._decorator;

@ccclass
export default class TravelEventRewardPageCtrl extends BasePageCtrl {

    travelEventType = null;
    reward = null;
    can_ggk = null;
    show_reward = null;

    onLoad() {
        this.onUILoad();
        this._animType = AnimType.SCALE;
        this._touchControl = false;
        this._hasPeneLock = true;
        this._hasBlack = true;
        this._hasTouchLock = false;
        this._hasBlackTouch = false;
        this.addButtonListen();
        super.onLoad();
    };

    start() { };

    onUILoad() {
        this.ui = this.node.addComponent(_);
    };

    _init(e?) {
        debugger
        if (e) {
            let t = e.video_type;
            let a = e.reward;
            let o = e.can_ggk;
            let i = e.show_reward;
            this.travelEventType = t;
            this.reward = a;
            if (this.travelEventType == TravelEvent.SCRAP_CARD) {
                this.can_ggk = o;
                this.show_reward = i;
                this.ui.cashLabel.getComponent(cc.Label).string = h.getCashBalance(this.reward) + "y";
            } else {
                this.ui.cashLabel.getComponent(cc.Label).string = h.getCashBalance(this.reward) + "y";
            }
            n.getInstance().playMusic("event_reward");
            this.initTitle();
            this.initBtnShow();
        }
    };

    initTitle() {
        for (let e of this.ui.titleNode.children) {
            e.active = 'title' + this.travelEventType === e.name;
        }
    };

    initBtnShow() {
        if (this.travelEventType == TravelEvent.SCRAP_CARD) {
            this.ui.btn_video.active = this.can_ggk;
            this.ui.btn_noScrap.active = this.can_ggk;
            this.ui.btn_revice.active = !this.can_ggk;
        } else {
            this.ui.btn_video.active = false;
            this.ui.btn_revice.active = true;
            this.ui.btn_noScrap.active = false;
        }
    };

    addButtonListen() {
        UiManager.addButtonListen(this.ui.btn_video, this.clickVideo, this);
        UiManager.addButtonListen(this.ui.btn_noScrap, this.clickReviceReward, this);
        UiManager.addButtonListen(this.ui.btn_revice, this.clickReviceReward, this);
    };

    clickVideo() {
        let e = this;
        p.log("播放视频======");
        let t = "TravelEventRewardPage_" + this.travelEventType;
        d.getInstance().playNormalVideoAd(function () {
            y.getVideoReward(e.travelEventType, 1, function () {
                e.clickClose();
            });
        }, function () { });
    };

    clickReviceReward() {
        let e = this;
        this.clickClose();
        if (this.travelEventType == TravelEvent.SCRAP_CARD && g.hasShowPage("ScrapingCardPage")) {
            g.hidePage("ScrapingCardPage");
        }
        c.trigger(u.SHOWEFFECT_FLYINGRED, {
            startPos: this.ui.redPaper.getPosition(),
            type: 0,
            callback: function () {
                if (e.travelEventType == TravelEvent.SCRAP_CARD) {
                    l.updateCashBalace(e.show_reward);
                } else {
                    l.updateCashBalace(e.reward);
                }
                e.scheduleOnce(function () {
                    s.checkNextOutSideGuide();
                    s.checkShowGameStartPage();
                }, 1);
            }
        });
    };

    clickClose() {
        this.hide();
    };
}

